//---------------------------------------------------------------------------

#include "vcl_stl.h"
#pragma hdrstop

#include "h_base.h"
#include "h_xml.h"
#include "h_global.h"
using namespace h_base;
using namespace h_xml;

#include "t_excludelistform.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TExcludeListForm *ExcludeListForm;
//---------------------------------------------------------------------------
__fastcall TExcludeListForm::TExcludeListForm(TComponent* Owner)
   : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TExcludeListForm::ButtonOkClick(TObject *Sender)
{
   OkFlag = true;
   Close();
}
//---------------------------------------------------------------------------
void __fastcall TExcludeListForm::ButtonCancelClick(TObject *Sender)
{
   Close();
}
//---------------------------------------------------------------------------
void __fastcall TExcludeListForm::GridAssign(TStringGrid *Grid,
   const set<UTF8String> &StrSet)
{
   Grid->ColWidths[0] = Grid->Width - 32;
   Grid->RowCount = StrSet.size();

   int i = 0;

   for ( set<UTF8String>::const_iterator Iter = StrSet.begin();
         Iter != StrSet.end(); Iter++, i++ )
   {
      Grid->Cells[0][i] = *Iter;
   }

   if ( i == 0 )
   {
      Grid->Cells[0][0] = "";
   }
}

bool __fastcall TExcludeListForm::EditExcludeList(set<UTF8String> &StrSet,
   const UTF8String &NewItem)
{
   EditItem->Text = NewItem;
   FStrSet = StrSet;
   GridAssign(Grid, FStrSet);

   OkFlag = false;
   ShowModal();

   if ( !OkFlag )
   {
      return false;
   }

   StrSet = FStrSet;
   return true;
}
void __fastcall TExcludeListForm::ButtonAddClick(TObject *Sender)
{
   UTF8String NewItem = EditItem->Text.TrimRight();

   if ( NewItem != "" )
   {
      FStrSet.insert(NewItem);
      GridAssign(Grid, FStrSet);
   }
}
//---------------------------------------------------------------------------
void __fastcall TExcludeListForm::ButtonClearClick(TObject *Sender)
{
   FStrSet.clear();
   GridAssign(Grid, FStrSet);
}
//---------------------------------------------------------------------------
void __fastcall TExcludeListForm::ButtonRemoveClick(TObject *Sender)
{
   int Top = Grid->Selection.Top;
   int Bottom = Grid->Selection.Bottom;

   if ( Top > Bottom )
   {
      return;
   }

   UTF8String Key;
   int i = 0;

   for ( set<UTF8String>::const_iterator Iter = FStrSet.begin();
         Iter != FStrSet.end(); Iter++, i++ )
   {
      if ( i == Top )
      {
         Key = *Iter;
         break;
      }
   }

   if ( Key != "" )
   {
      FStrSet.erase(Key);
      GridAssign(Grid, FStrSet);
   }
}
//---------------------------------------------------------------------------

